package com.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;



import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.pojo.User;
import com.Dao.UserDaox;
import com.google.gson.Gson;


@WebServlet("/Fileupload2")
public class Fileupload2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
       

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws  IOException {
		try {
			// 1. ������������
			FileItemFactory factory = new DiskFileItemFactory();
			// 2. �ļ��ϴ����Ĺ�����
			ServletFileUpload upload = new ServletFileUpload(factory);
			// ���ô�С���Ʋ���
			upload.setFileSizeMax(10*1024*1024);// �����ļ���С���ƣ��˴�Ϊ10M
			upload.setSizeMax(500*1024*1024);	// ���ļ���С���ƣ��˴�Ϊ500M
			upload.setHeaderEncoding("UTF-8");	// �������ļ����봦��
			// 3. ����������ת��Ϊlist����
			List<FileItem> list = upload.parseRequest(request);
			// ����
			for(FileItem item : list){
				// �жϣ���ͨ�ı�����
				if (item.isFormField()){
					// ��ȡ����
					String name = item.getFieldName();
					// ��ȡֵ
					String value = item.getString();
					//ת��ΪUTF-8����
					value = new String(value.getBytes("ISO8859-1"),"UTF-8");
					System.out.println(name+"��"+value);
				} 
				// �ļ�����
				else {
					/******** �ļ��ϴ� ***********/
					// a. ��ȡ�ļ�����
					String name = item.getName();
					String fileName = null;
	                int ind = name.lastIndexOf("\\");
	                if (ind != -1) {
	                    fileName = name.substring(ind + 1);
	                }else {
	                    fileName = name;
	                }
					// a1. �ȵõ�Ψһ���(���ɵ�Ψһ�ַ���������������������Ѿ����ڵ��ļ�ͬ��)
					String id = UUID.randomUUID().toString();
					// a2. ƴ���ļ���
					name = id + fileName;	
					// b. �õ��ϴ�Ŀ¼
					String basePath=getServletContext().getRealPath("/file");
					String basePathx="F:\\apache-tomcat-9.0.5-windows-x86\\apache-tomcat-9.0.5\\wtpwebapps\\file";
					// c. ����Ҫ�ϴ����ļ�����
					File file = new File(basePath,name);
					File filex = new File(basePathx,name);
					// d. �ϴ�
					System.out.println(filex);
					item.write(file);
					UserDaox userDao =new UserDaox(); 
					userDao.findpasd(2, name);
					Gson gson = new Gson();
					String jsonStr = gson.toJson(name);
					PrintWriter out = response.getWriter();
					out.print(jsonStr);
				}
				
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
